package com.just.fun.arithmetic;

import java.util.Stack;

/**
 * 两个栈实现一个队列
 */
public class Stack2Queue {
    private Stack<Integer> stack1=new Stack<>();
    private Stack<Integer> stack2=new Stack<>();

    //入栈
    public void push(int node){
        stack1.push(node);
    }

    //出栈
    public int pop() throws Exception {
        if(stack1.isEmpty() && stack2.isEmpty()){
            throw new Exception("栈为空");
        }
        if(stack2.isEmpty()){
            while (!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }

    public static void main(String[] args) throws Exception {
        Stack2Queue stack2Queue=new Stack2Queue();
        stack2Queue.push(1);
        stack2Queue.push(2);
        stack2Queue.push(3);
        System.out.println(stack2Queue.pop());
    }
}
